import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
const constantRoutes = [
  {
    path: "/reg",
    component: () => import("../views/reg/index")
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true
  }
];

export const asyncRouterMap = [
  // 平台数据
  {
    path: "/Dashboard",
    component: Layout,
    redirect: "/dashboard",
    meta: { title: "", icon: "el-icon-menu", children: ["/dashboard"] },
    children: [
      {
        path: "/dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "后台数据", icon: "dashboard" }
      }
    ]
  },
  // 平台管理员管理
  {
    path: "/Admins",
    component: Layout,
    meta: {
      title: "平台管理员管理",
      icon: "el-icon-menu",
      children: ["/admins"]
    },
    children: [
      {
        path: "/admins",
        name: "Admins",
        component: () => import("@/views/admins/index"),
        meta: { title: "管理员模块", icon: "el-icon-s-check" }
      }
    ]
  },

  // 用户管理
  {
    path: "/Users",
    component: Layout,
    meta: { title: "用户管理", icon: "el-icon-menu", children: ["/users"] },
    children: [
      {
        path: "/users",
        name: "Users",
        component: () => import("@/views/users/index"),
        meta: { title: "用户模块", icon: "el-icon-user" }
      }
    ]
  },
  //商家数据统计
  {
    path: "/Business",
    component: Layout,
    meta: { title: "数据统计", icon: "el-icon-menu", children: ["/business"] },
    children: [
      {
        path: "/business",
        name: "Business",
        component: () => import("@/views/business/index"),
        meta: { title: "商家数据", icon: "dashboard" }
      }
    ]
  },
  // 门店管理
  {
    path: "/Shops",
    component: Layout,
    redirect: "/shops",
    meta: { title: "门店管理", icon: "el-icon-menu", children: ["/shops"] },
    children: [
      {
        path: "/shops",
        name: "Shops",
        component: () => import("@/views/shops/index"),
        meta: { title: "门店模块", icon: "el-icon-s-shop" }
      }
    ]
  },
  // 商品管理
  {
    path: "/Goods",
    component: Layout,
    meta: { title: "商品管理", icon: "el-icon-menu", children: ["/goods"] },
    children: [
      {
        path: "/goods",
        name: "Goods",
        component: () => import("@/views/goods/index"),
        meta: { title: "商品模块", icon: "el-icon-s-goods" }
      }
    ]
  },

  // 服务管理
  {
    path: "/Services",
    component: Layout,
    meta: { title: "服务管理", icon: "el-icon-menu", children: ["/services"] },
    children: [
      {
        path: "/services",
        name: "Services",
        component: () => import("@/views/services/index"),
        meta: { title: "服务模块", icon: "el-icon-s-claim" }
      }
    ]
  },
  // 门店申请管理
  {
    path: "/ShopsApply",
    component: Layout,
    redirect: "/shopsApply/shopsApply",
    name: "ShopsApply",
    meta: {
      title: "门店申请管理",
      icon: "el-icon-menu",
      children: ["/shopsApply", "/shopsUsersApply"]
    },
    children: [
      {
        path: "/shopsApply",
        component: () => import("@/views/shopsApply/shopsApply/index"),
        name: "ShopsApply",
        meta: { title: "店铺申请", icon: "el-icon-office-building" }
      },
      {
        path: "/shopsUsersApply",
        component: () => import("@/views/shopsApply/shopsUsersApply/index"),
        name: "shopsUsersApply",
        meta: { title: "管理员申请", icon: "el-icon-user" }
      }
    ]
  },
  // 订单管理
  {
    path: "/Orders",
    component: Layout,
    redirect: "/orders/usersService",
    name: "Orders",
    meta: {
      title: "订单管理",
      icon: "el-icon-menu",
      children: ["/usersService", "/goodsView"]
    },
    children: [
      {
        path: "/usersService",
        component: () => import("@/views/orders/usersService/index"),
        name: "UsersService",
        meta: { title: "用户服务", icon: "el-icon-user" }
      },
      {
        path: "/goodsView",
        component: () => import("@/views/orders/goodsView/index"),
        name: "GoodsView",
        meta: { title: "商品查看", icon: "el-icon-goods" }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true }
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
